/*
   Capture screens
   Created by Randall Maas, February 20 2009
   Copyright (c) 2009, Randall Maas
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

   Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

   Neither the name of Blackwood Designs, LLC nor the names of its contributors
   may be used to endorse or promote products derived from this software without
   specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

static partial class Program
{
   internal static Bitmap[] GrabScreens()
   {
      // an array of bitmaps, one per monitor on the system
      Screen[] AllScreens = Screen . AllScreens;
      Bitmap[] screenBitmap = new Bitmap[AllScreens . Length];

      for (int i = 0; i < AllScreens.Length; i++)
      {
         // grab the size of the current monitor
         Rectangle region = AllScreens[i] . Bounds;

         // create a bitmap of that size
         screenBitmap[i] = new Bitmap(region.Width, region.Height, PixelFormat.Format32bppArgb);

         // copy the current screen image to the bitmap for the current monitor
         using (Graphics bitmapGraphics = Graphics.FromImage(screenBitmap[i]))
          bitmapGraphics.CopyFromScreen(region.Left, region.Top, 0, 0, region.Size);
      }

      return screenBitmap;
   }


   /// <summary>
   /// Retrieves the visible area of the screen
   /// </summary>
   /// <returns></returns>
   internal static Rectangle ScreenRectangle()
   {
      // an array of bitmaps, one per monitor on the system
      Screen[] AllScreens = Screen . AllScreens;
      int X     = 0, Y = 0;
      int Right = 0;
      int Bottom= 0;
      for (int i = 0; i < AllScreens.Length; i++)
      {
         // grab the size of the current monitor
         Rectangle region = AllScreens[i] . Bounds;
         if (region . X < X)
           X = region . X;
         if (region . Y < Y)
           Y = region . Y;
         if (region . Right > Right)
           Right = region . Right;
         if (region . Bottom > Bottom)
           Bottom = region . Bottom;
      }

      // Create the rectangle to represent the screen
      return new Rectangle(X,Y, Right-X, Bottom-Y);
   }

#if false
   /// <summary>
   /// This creates a copy of all the screens as a single bitmap
   /// </summary>
   /// <returns>The bitmap</returns>
   internal static Bitmap CaptureScreen()
   {
      Rectangle R = ScreenRectangle();

      // create a bitmap of that size
      Bitmap screenBitmap = new Bitmap(R . Width, R . Height, PixelFormat.Format32bppArgb);

      // copy the current screen image to the bitmap for the current monitor
      using(Graphics bitmapGraphics = Graphics.FromImage(screenBitmap))
       bitmapGraphics.CopyFromScreen(R . X, R . Y, 0, 0, R . Size, CopyPixelOperation.SourceCopy);

      return screenBitmap;
   }
#endif
}
